<?php
/**
 *  awe 云逛展，针对「近7天活跃但未浏览过活动页」的用户，进行精准push，引导参与活动
 */
ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Doraemon\model\Es\UserPortrait;
use Doraemon\service\Push\PushService;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\LinkFormat;

$actionUids = [];//已经参与云逛展的用户
$rows = [];
$scrollId = '';
$pushedUids = [];
$i = 1;
do {
    $res = UserPortrait::getLoginDays7(5000, $scrollId);
    var_dump('查询es第' . $i . '次，查询数量：' . count($res['rows']));
    $rows = ($res && $res['rows']) ? $res['rows'] : [];
    $uids = array_column($rows, 'uid');
    $scrollId = ($res && $res['scroll_id']) ? $res['scroll_id'] : '';

    foreach ($uids as $uid) {
        if (!in_array($uid, $actionUids) && !in_array($uid, $pushedUids)) {
            array_push($pushedUids, $uid);
            pushMsg($uid);
        }
    }
    $i++;
} while ($rows);
UserPortrait::clear_scroll($scrollId);


function pushMsg($uid)
{
    var_dump($uid);
    $link = Hhzurl::getDomain() . '/shop/activity-page/activity-page?page_id=659';
    $link = LinkFormat::encryptionLink($link);
    $pushId = 'brand_awe_' . date('Y-m-d');
    $params = [
        'uid' => $uid,
        'title' => '高达¥8999的奖品等你来抽！',
        'alert' => '参与AWE云逛展，第一时间了解展会亮点，为喜爱品牌投票还能参与抽奖哦，最后10小时，快来 >>',
        'param' => [
            'link' => $link,
            'push_id' => $pushId,
            'image' => ''
        ]
    ];
    PushService::send($params);

    $data = [
        'msgCode' => 0,
        'to_uid' => $uid,
        'msgParams' => [
            'title' => '大家都在玩：参与投票赢大奖',
            'detail' => '好好住带你一起参加 AWE 云逛展，为喜爱品牌投票就能参与抽奖，最高价值¥8999奖品等你来抽！',
            'img' => 'https://img.haohaozhu.cn/Op-imageShow/747c520ku0aq04000qqgpzo?iv=1',
            'link' => $link,
            'type' => 2,
        ]
    ];
    \Doraemon\cache\Queue::insertQueue('queue/Sysmsg/Send', $data, \Doraemon\cache\Queue::QUEUE_DORAEMON);
}